package org.ilead.iserve.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.ilead.iserve.model.HelpCategory;
import org.ilead.iserve.model.HelpInformation;
import org.ilead.iserve.model.HelpInformationMessage;
import org.ilead.iserve.model.User;
import org.ilead.iserve.model.base.PageView;
import org.ilead.iserve.service.IHelpCategoryService;
import org.ilead.iserve.service.IHelpInformationMessageService;
import org.ilead.iserve.service.IHelpInformationService;
import org.ilead.iserve.service.IHelpPageService;
import org.ilead.iserve.service.manager.UserManager;
import org.ilead.iserve.web.base.BaseActionSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

public class HelpAction extends BaseActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory
	.getLogger(HelpAction.class);
	private IHelpInformationService helpInformationService;
	@Autowired
	private IHelpPageService helpPageService;
	@Autowired
	private IHelpInformationMessageService helpInformationMessageService;
	@Autowired
	private IHelpCategoryService helpCategoryService;
	private HelpInformation helpInformation = null;
	private HelpInformationMessage helpInformationMessage = null;
	private List<HelpInformationMessage> helpInformationMessageList = new ArrayList<HelpInformationMessage>();
	private Long id ;
	private List<HelpInformation> helpInformationList  = new ArrayList<HelpInformation>();
	private String categoryId;
	//String userId = "whf";
//	String userName = "huafeng";
	private Long subCategoryId;
	private List<HelpCategory> helpCategoryList;
	private List<HelpCategory> helpSubCategoryList;
	private HelpCategory helpCategory;
//	private String subCategoryId;
	/*
	 * 显示所有信息
	 */
	public String listHelpInformationAll() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		int maxResult = 20;
	
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("modifyTime", "desc");
		
		PageView<HelpInformation> pageView = new PageView<HelpInformation>(maxResult, getPage());
		String sql=" status = '1' ";
		
		if(categoryId!=null){
			sql+="and categoryId in ("+categoryId+")";
		}
		pageView.setQueryResult(helpPageService.getScrollData(pageView.getFirstResult(), maxResult,sql,null,orderby));
		request.setAttribute("pageView", pageView);
		return SUCCESS;		
	}
	/*
	 * 显示用户信息
	 */
	
	public String listHelpInformation() throws Exception{
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		User user = UserManager.getInstance().getLoginUser();
		int maxResult = 20;
	
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("modifyTime", "desc");
		
		PageView<HelpInformation> pageView = new PageView<HelpInformation>(maxResult, getPage());
		String sql=" userId=? ";
		if(categoryId!=null){
			sql+="and categoryId in ("+categoryId+")";
		}
		pageView.setQueryResult(helpPageService.getScrollData(pageView.getFirstResult(), maxResult,sql,new Object[]{user.getLoginName()},orderby));
		request.setAttribute("pageView", pageView);
		return SUCCESS;		
	}
	public String showHelpInformation() throws Exception {
		if(id <= 0) {
			return ERROR;
		}
		helpInformation = helpInformationService.findById(id);
		if(helpInformation == null)
			return ERROR;
		return SUCCESS;
	}
	public String detailHelpInformation() throws Exception{
		helpInformation = helpInformationService.findById(id);
		
		helpInformationMessageList = helpInformationMessageService.findByTopicId(helpInformation.getId());
		return SUCCESS;
	}
	public String viewHelpInformation() throws Exception{
		helpCategoryList = helpCategoryService.findByCategoryPid(new Long(-1));
		helpInformation = helpInformationService.findById(id);
		helpCategory = helpCategoryService.findByCategoryId(new Long(helpInformation.getCategoryId()));
		if(helpCategory.getCategoryPid()!=-1){
			helpSubCategoryList = helpCategoryService.findByCategoryPid(helpCategory.getCategoryPid());
		}else{
			helpCategory.setCategoryPid(helpCategory.getCategoryId());
		}
		return SUCCESS;
	}
	public String addHelpInformation() throws Exception {
		User user = UserManager.getInstance().getLoginUser();
		if(subCategoryId!=null&&subCategoryId!=0)
			helpInformation.setCategoryId(subCategoryId.toString());
		HelpCategory helpCategory = helpCategoryService.findByCategoryId(new Long(helpInformation.getCategoryId()));
		helpInformation.setCreateTime(new Date());
		helpInformation.setModifyTime(new Date());
		helpInformation.setStatus("1");
		helpInformation.setUserId(user.getLoginName());
		helpInformation.setCategoryName(helpCategory.getCategoryName());
		helpInformation.setUserName(user.getUserName());
		helpInformationService.save(helpInformation);
		return SUCCESS;
	}
	public String updateHelpInformation() throws Exception {
		User user = UserManager.getInstance().getLoginUser();
		if(subCategoryId!=null&&subCategoryId!=0)
			helpInformation.setCategoryId(subCategoryId.toString());
		HelpInformation helpInformationNew = helpInformationService.findById(helpInformation.getId());
		HelpCategory helpCategory = helpCategoryService.findByCategoryId(new Long(helpInformation.getCategoryId()));
		helpInformationNew.setModifyTime(new Date());
		helpInformationNew.setCategoryId(helpInformation.getCategoryId());
		helpInformationNew.setTitle(helpInformation.getTitle());
		helpInformationNew.setContact(helpInformation.getContact());
		helpInformationNew.setContent(helpInformation.getContent());
		helpInformationNew.setStatus(helpInformation.getStatus());
		helpInformationNew.setUserName(user.getUserName());
		helpInformationNew.setCategoryName(helpCategory.getCategoryName());
		helpInformationService.update(helpInformationNew);
		return SUCCESS;
	}
	
	public String publish() throws Exception {
		helpCategoryList = helpCategoryService.findByCategoryPid(new Long(-1));
		return SUCCESS;
	}
	public String delHelpInformation() throws Exception {
		 helpInformationService.delete(id);
		return SUCCESS;
	}
	public void setHelpInformationService(
			IHelpInformationService helpInformationService) {
		this.helpInformationService = helpInformationService;
	}
	public void setHelpPageService(IHelpPageService helpPageService) {
		this.helpPageService = helpPageService;
	}

	public HelpInformation getHelpInformation() {
		return helpInformation;
	}

	public void setHelpInformation(HelpInformation helpInformation) {
		this.helpInformation = helpInformation;
	}

	public List<HelpInformation> getHelpInformationList() {
		return helpInformationList;
	}

	public void setHelpInformationList(List<HelpInformation> helpInformationList) {
		this.helpInformationList = helpInformationList;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public List<HelpInformationMessage> getHelpInformationMessageList() {
		return helpInformationMessageList;
	}

	public void setHelpInformationMessageList(
			List<HelpInformationMessage> helpInformationMessageList) {
		this.helpInformationMessageList = helpInformationMessageList;
	}

	public HelpInformationMessage getHelpInformationMessage() {
		return helpInformationMessage;
	}

	public void setHelpInformationMessage(
			HelpInformationMessage helpInformationMessage) {
		this.helpInformationMessage = helpInformationMessage;
	}

	public void setHelpInformationMessageService(
			IHelpInformationMessageService helpInformationMessageService) {
		this.helpInformationMessageService = helpInformationMessageService;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public void setHelpCategoryService(IHelpCategoryService helpCategoryService) {
		this.helpCategoryService = helpCategoryService;
	}

	public Long getSubCategoryId() {
		return subCategoryId;
	}

	public void setSubCategoryId(Long subCategoryId) {
		this.subCategoryId = subCategoryId;
	}

	public List<HelpCategory> getHelpCategoryList() {
		return helpCategoryList;
	}

	public void setHelpCategoryList(List<HelpCategory> helpCategoryList) {
		this.helpCategoryList = helpCategoryList;
	}

	public HelpCategory getHelpCategory() {
		return helpCategory;
	}

	public void setHelpCategory(HelpCategory helpCategory) {
		this.helpCategory = helpCategory;
	}

	public List<HelpCategory> getHelpSubCategoryList() {
		return helpSubCategoryList;
	}

	public void setHelpSubCategoryList(List<HelpCategory> helpSubCategoryList) {
		this.helpSubCategoryList = helpSubCategoryList;
	}
}
